<!DOCTYPE html>
<meta charset="utf-8">
<script src="/bower_components/webcomponentsjs/webcomponents-loader.js"></script>
<script src="/bower_components/web-component-tester/browser.js"></script>
<link rel="import" href="/bower_components/iron-test-helpers/iron-test-helpers.html">
<link rel="import" href="/elements/som-bug-queue/som-bug-queue.html">
<test-fixture id="basic">
  <template>
    <som-bug-queue></som-bug-queue>
  </template>
</test-fixture>
<script>
(function() {
  'use strict';

  suite('basic tests', function() {
    var element;
    var server;
    var responseHeaders = {
        json: {'Content-Type': 'application/json'},
    };

    setup(function() {
      element = fixture('basic');
      server = sinon.fakeServer.create();
    });

    teardown(function() {
      server.restore();
    });

    test('_haveNoBugs', function() {
      assert.equal(true, element._haveNoBugs());
      assert.equal(true, element._haveNoBugs([]));
      assert.equal(false,
          element._haveNoBugs([{id:1, status: 'foo'}]));
    });

    test('renders empty', function(done) {
      assert.equal(true, element.$.main.hidden);
      element._bugQueueJson = {};
      assert.equal(true, element._haveNoBugs(element._bugs));
      flush( () => {
        assert.equal(true, element.$.main.hidden);
        done();
      });
    });

    test('renders basic', function(done) {
      assert.equal(true, element.$.main.hidden);

      // Bug queue gets unhidden when we switch to a tree with a bug queue label
      element.bugQueueLabel = 'test';
      assert.equal(false, element.$.main.hidden);

      element._bugQueueJson = {
        items: [
          { summary: 'foo', status: 'bar', id: 123 },
          { summary: 'baz', status: 'zippy', id: 456 },
        ],
      };
      element._bugsLoaded = true;

      assert.equal(false, element._haveNoBugs(element._bugQueueJson));

      flush( () => {
        assert.equal(false, element.$.main.hidden);
        assert.equal(2, element.$.main.querySelectorAll('.bug').length);
        done();
      });
    });

    test('_computeBugs', function() {
      let bugQueueJson = {
        items: [
          { summary: 'foo', status: 'bar', id: 123 },
          { summary: 'baz', status: 'zippy', id: 456 },
        ],
      };
      let uncachedBugsJson = {
        items: [
          { summary: 'hello', status: 'world', id: 555 },
          { summary: 'let us', status: 'test', id: 987 },
        ],
      };

      assert.deepEqual([
        { summary: 'foo', status: 'bar', id: 123 },
        { summary: 'baz', status: 'zippy', id: 456 },
      ], element._computeBugs(bugQueueJson, null));

      assert.deepEqual([
        { summary: 'hello', status: 'world', id: 555 },
        { summary: 'let us', status: 'test', id: 987 },
      ], element._computeBugs(null, uncachedBugsJson));

      assert.deepEqual([
        { summary: 'hello', status: 'world', id: 555 },
        { summary: 'let us', status: 'test', id: 987 },
      ], element._computeBugs(bugQueueJson, uncachedBugsJson));
    });

    test('_computePriority parses priorities', function() {
      let bugs = [
        { summary: 'foo', status: 'bar', id: 123, labels: ['Pri-2'] },
        { summary: 'baz', status: 'zippy', id: 456, labels: ['Pri-0'] },
        { summary: 'baz', status: 'zippy', id: 456, labels: ['Pri-1'] },
        { summary: 'baz', status: 'zippy', id: 456, labels: [] },
      ];
      assert.equal(2, element._computePriority(bugs[0]));
      assert.equal(0, element._computePriority(bugs[1]));
      assert.equal(1, element._computePriority(bugs[2]));
      assert.equal(element.UNSET_PRIORITY, element._computePriority(bugs[3]));
    });

    test('_computeBugsByPriority returns proper bug list', function() {
      let bugs = [
        { summary: 'this is', labels: ['Pri-2'] },
        { summary: 'hello', labels: ['Pri-0'] },
        { summary: 'world', labels: ['Pri-1'] },
        { summary: 'ordered', labels: [] },
      ];


      let result = element._computeBugsByPriority(bugs);
      assert.equal(0, result[0].priority);
      assert.equal(1, result[1].priority);
      assert.equal(2, result[2].priority);
      assert.equal(element.UNSET_PRIORITY, result[3].priority);

      assert.deepEqual([{ summary: 'hello', labels: ['Pri-0'] }], result[0].bugs);
      assert.deepEqual([{ summary: 'world', labels: ['Pri-1'] }], result[1].bugs);
      assert.deepEqual([{ summary: 'this is', labels: ['Pri-2'] }], result[2].bugs);
      assert.deepEqual([{ summary: 'ordered', labels: [] }], result[3].bugs);
    });
  });
})();
</script>
